import axios from 'axios'
import config from '@/config'

const env = process.env.NODE_ENV == 'production' ? 'prod' : 'dev'
const token = ''

// create an axios instance
const instance = axios.create({
    baseURL: config[env].pharstationUrl,
    method: 'GET',
    headers: {
        'Content-Language': 'zh',
        'Content-Type': 'application/json;charset=UTF-8'
    },
    // withCredentials: true, // send cookies when cross-domain requests
    timeout: 15000
})

// request interceptor
instance.interceptors.request.use(
    (config: any) => {
        // todo... 做一些数据校验和session的校验
        if (token) config.headers.Authorization = `Bearer ${token}`
        return config
    },
    (error: any) => {
        console.log('request error：', error)
        return Promise.reject(error)
        // return error
    }
)

// response interceptor
instance.interceptors.response.use(
    (response: any) => {
        return response.data
    },
    (error: any) => {
        let res = error.response || { status: 500, statusText: error.message }
        return Promise.reject(res)
    }
)

export default instance.request
